﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RooHui.Infrastructure.Entities.Entity;
using RooHui.Tool;
using RooHui.Infrastructure.Entities.EFContext;
using RooHui.Infrastructure.Entities.Enum;
using RooHui.Infrastructure.Entities.Repository;

namespace RooHui.OA.Biz
{
    public class ProjectAuditBiz
    {
        /// <summary>
        /// 分页获取项目审批信息
        /// </summary>
        /// <param name="page"></param>
        /// <param name="pageSize"></param>
        /// <param name="guaranteeCompanyID"></param>
        /// <param name="key"></param>
        /// <param name="count"></param>
        /// <returns></returns>
        public static List<Project> GetProjectListForAudit(int page, int pageSize,int guaranteeCompanyID,string key,string userName, ref int count)
        {
            try
            {
                using (Context ctx = new Context())
                {
                    var expression = from c in ctx.Project
                                     join d in ctx.Product
                                     on c.ProductID equals d.ID
                                     join e in ctx.GuaranteeCompany
                                     on c.GuaranteeCompanyID equals e.ID
                                     join f in ctx.Enterprise
                                     on c.EnterpriseID equals f.ID
                                     join k in ctx.ProjectPolicy
                                     on c.ID equals k.ProjectId
                                     join j in ctx.Policy.Where(m=>m.IsAttach==false)
                                     on k.PolicyId equals j.ID
                                     join g in ctx.ProcessDoing.Where(h => h.DoingState == 0 && h.SignUsers == userName)
                                     on c.ID equals g.ProjectID
                                     where (e.ID == guaranteeCompanyID || guaranteeCompanyID==-1)
                                     &&(c.ProjectCode.Contains(key)||c.ProjectTitle.Contains(key)
                                     ||d.ProductName.Contains(key)||f.EnterpriseCode.Contains(key)
                                     ||f.EnterpriseName.Contains(key))
                                     &&(c.Status==(int)ProjectStatus.Auditing||c.Status==(int)ProjectStatus.Submit)
                                     orderby c.CreateTime
                                     select new { c, d, e, f,j};
                    count = expression.Count();
                    var data = expression
                        .Skip((page - 1) * pageSize)
                        .Take(pageSize)
                        .ToList().Select(p => new Project().Set(p.c).LoadProduct(p.d).LoadGuaranteeCompany(p.e).LoadEnterprise(p.f).LoadPolicy(p.j)).ToList();
                    return data;
                }
            }
            catch (Exception ex)
            {
                LogHelper.Write(CommonLogger.Web, ex);
            }
            return null;
        }
        /// <summary>
        /// 根据项目ID获取项目相关资料
        /// </summary>
        /// <param name="projectID"></param>
        /// <returns></returns>
        public static Project GetProjectByIDForAudit(int projectID)
        {
            try
            {
                using (Context ctx = new Context())
                {
                    var expression = from c in ctx.Project
                                     join d in ctx.Product
                                     on c.ProductID equals d.ID
                                     join e in ctx.GuaranteeCompany
                                     on c.GuaranteeCompanyID equals e.ID
                                     join f in ctx.Enterprise
                                     on c.EnterpriseID equals f.ID
                                     join u in ctx.User
                                     on c.CreateUser equals u.UserName
                                     where c.ID==projectID
                                     orderby c.CreateTime
                                     select new { c, d, e, f,u.RealName};
                    
                    var data = expression.ToList().Select(p => new Project().Set(p.c).LoadProduct(p.d).LoadGuaranteeCompany(p.e).LoadEnterprise(p.f).LoadUser(new U_User(){RealName=p.RealName})).FirstOrDefault();
                   //获取政策信息
                    var policyList = (from c in ctx.Policy
                                      join d in ctx.ProjectPolicy
                                      on c.ID equals d.PolicyId
                                      where d.ProjectId == projectID
                                      select new { c, d })
                                    .ToList()
                                    .Select(p => new Policy().Set(p.c)).ToList();
                    data.PolicyList = policyList;
                    //获取相关资料信息
                    var resourceList = new Resource().FindAll(c => c.Type == (int)ResourceType.ProjectResource && c.RefId == projectID);
                    data.ResourceList = resourceList;
                    return data;
                }
            }
            catch (Exception ex)
            {
                LogHelper.Write(CommonLogger.Web, ex);
            }
            return null;
        }
    }
}
